package com.demo.connectdbtest.datasourceconfig;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 描述：
 *
 * @author: chengc
 * @date 2021/03/31
 */
@Configuration
@MapperScan(basePackages = "com.demo.connectdbtest.module.*.popoMapper", sqlSessionTemplateRef="cmsSqlSessionTemplate")
public class ConfigurationDatasourcePopo {
    private static final Logger log = LoggerFactory.getLogger(ConfigurationDatasourcePopo.class);

    @Value("${spring.datasource.slave.url}")
    private String url;
    @Value("${spring.datasource.slave.password}")
    private String password;
    @Value("${spring.datasource.slave.username}")
    private String username;
    @Value("${spring.datasource.slave.driver-class-name}")
    private String driverClassName;



    @Bean(name = "cmsDataSource")
    //@ConfigurationProperties("spring.datasource.slave")
    public DataSource slaveDataSource(){
        log.info("url={},pwd={},username={},driver={}",url,password,username,driverClassName);
        return DataSourceBuilder.create().url(url).username(username).password(password).driverClassName(driverClassName).build();

        //return DataSourceBuilder.create().build();
    }

    @Bean(name = "cmsSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("cmsDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/popoMapper/*.xml");
        sessionFactoryBean.setMapperLocations(resources);
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        sessionFactoryBean.setConfiguration(configuration);
        return sessionFactoryBean.getObject();
    }
    @Bean(name = "cmsTransactionManager")
    public PlatformTransactionManager cmsTransactionManager(@Qualifier("cmsDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "cmsSqlSessionTemplate")
    public SqlSessionTemplate cmsSqlSessionTemplate(@Qualifier("cmsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
